﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace Calc
{
    public partial class frmNote : Form
    {
        public frmNote()
        {
            InitializeComponent();
        }
        ManageItems objManageItems = new ManageItems();
        private void frmNote_Load(object sender, EventArgs e)
        {
            if (ProjectDB.ID != 0)
            {
                lblDescription.Text = ProjectDB.Desctiption;
                SqlCeConnection cn = new SqlCeConnection(objManageItems.GetConnectionString());
                SqlCeCommand cmd=new SqlCeCommand("Select Note from MeasurementChild where ID="+ProjectDB.ID);
                cmd.Connection = cn;
                DataSet ds=new DataSet();
                SqlCeDataAdapter da=new SqlCeDataAdapter(cmd);
                da.Fill(ds);
                if (ds.Tables[0].Rows.Count>0)
                    txtmsg.Text=ds.Tables[0].Rows[0]["Note"].ToString();
            }
            else
            {
                SqlCeConnection cn = new SqlCeConnection(objManageItems.GetConnectionString());
                SqlCeCommand cmd = new SqlCeCommand("Select Note from Measurements where MeasurementID=" + ProjectDB.MeasurementID);
                cmd.Connection = cn;
                DataSet ds=new DataSet();
                SqlCeDataAdapter da=new SqlCeDataAdapter(cmd);
                da.Fill(ds);
                if (ds.Tables[0].Rows.Count>0)
                    txtmsg.Text=ds.Tables[0].Rows[0]["Note"].ToString();
                lblDescription.Text = "";
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            SqlCeConnection cn = new SqlCeConnection(objManageItems.GetConnectionString());
            SqlCeCommand cmd = new SqlCeCommand();
            if (ProjectDB.ID != 0)
            {
                cmd.CommandText = "Update MeasurementChild set Note='" + txtmsg.Text + "' where ID=" + ProjectDB.ID;
            }
            else if (ProjectDB.MeasurementID != 0)
            {
                cmd.CommandText = "Update Measurements set Note='" + txtmsg.Text + "' where MeasurementID=" + ProjectDB.MeasurementID;
            }
            cmd.Connection = cn;
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
            this.Dispose();
        }
    }
}
